package view.layer
{
	import control.manager.MapManager;
	
	import data.MapData;
	
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import ghostcat.display.game.Display45Util;

	/**
	 * 网格层 
	 * @author leo
	 * 
	 */	
	public class GridLayer extends BaseLayer
	{
		private var mapData :MapData;
		/**
		 *  网格层
		 */		
		public function GridLayer(mapData :MapData)
		{
			this.mapData = mapData;
			super();
			initGridLayer();
		}
		
		
		private function initGridLayer():void
		{
			var g :Graphics = this.graphics;
			var walkRows :int = this.mapData.mapTileRows;
			var walkCols :int = this.mapData.mapTileCols;
			
			var walkW :int = mapData.mapWalkGapWidth;
			var walkH :int = mapData.mapWalkGapHeight;
			
			var mapW :int = walkRows * walkW;
			var mapH :int = walkCols * walkH;
			
			g.lineStyle(1,0xff0000);
			
			//矩形线画法
			var col:int =  Math.floor(this.mapData.mapWidth / walkW);
			var row:int =  Math.round(this.mapData.mapHeight / walkH);
			
			var wHalfTile :int = int(walkW/2);
			var hHalfTile :int = int(walkH/2);
			var dblMapWidth:int = col*2 + 3;
			var dblMapHeight:int = row*2 + 3;
			for (var i:int=1; i<dblMapWidth; i = i+2)
			{
				
				g.moveTo( i*wHalfTile, 0 );
				if (dblMapHeight+i >= dblMapWidth)
				{
					g.lineTo( dblMapWidth*wHalfTile, (dblMapWidth-i)*hHalfTile );
				}
				else
				{
					g.lineTo( (dblMapHeight+i)*wHalfTile, dblMapHeight*hHalfTile );
				}
				
				g.moveTo( i*wHalfTile, 0 );
				if (i <= dblMapHeight)
				{
					g.lineTo( 0, i*hHalfTile );
				}
				else
				{
					g.lineTo( (i - dblMapHeight)*wHalfTile, dblMapHeight*hHalfTile );//i-row-1
				}
			}
			
			for (var j:int=1; j<dblMapHeight; j=j+2)
			{
				
				g.moveTo( 0, j*hHalfTile );
				if (dblMapHeight-j >= dblMapWidth)
				{
					g.lineTo( dblMapWidth*wHalfTile, (dblMapWidth+j)*hHalfTile );
				}
				else
				{
					g.lineTo( (dblMapHeight-j)*wHalfTile, dblMapHeight*hHalfTile );
				}
			}
			
			for (var m:int=0; m<dblMapHeight; m=m+2)
			{
				g.moveTo( dblMapWidth*wHalfTile, m*hHalfTile );
				if (dblMapWidth-dblMapHeight+m < 0)
				{
					g.lineTo( 0, (dblMapWidth+m)*hHalfTile );
				}
				else
				{
					g.lineTo( (dblMapWidth-dblMapHeight+m)*wHalfTile, dblMapHeight*hHalfTile );
				}
			}
			
			if(this.mapData.mapWalkAry.length == 0) return;
			var aryRows :int = this.mapData.mapWalkAry.length;
			var aryCols :int = this.mapData.mapWalkAry[0].length;
			
			for(var txtRow :int = 0; txtRow < aryRows; txtRow++)
			{
				for(var txtCol :int = 0; txtCol < aryCols; txtCol++)
				{
					var w :int = mapData.mapWalkGapWidth;
					var h :int = mapData.mapWalkGapHeight;
					var p :Point = MapManager.instance.getPixelPoint(w,h,txtCol,txtRow);
					var txt :TextField = new TextField();
					txt.selectable = txt.border = false;
					txt.defaultTextFormat = new TextFormat(null,15,0,null,null,null,null,null,"center");
					txt.text =  txtCol+ "," + txtRow;
					txt.width = 50;
					txt.height = 30;
					txt.x = p.x - 25;
					txt.y = p.y - 10;
					addChild(txt);
				}
			}
			
			
//------------------------------------------------------------------------------------------------------------------------------------>			
			
			
//			//菱形画法
//			//Y
//			for(var row :int = 0; row <= walkRows; row++)
//			{
//				var dx :Number = row * walkW;
//				var p1 :Point = new Point(dx,0);
//				p1 = Display45Util.trans90To45(p1);
//				var p2 :Point = new Point(dx,mapH);
//				p2 = Display45Util.trans90To45(p2);
//				g.moveTo(p1.x,p1.y);
//				g.lineTo(p2.x,p2.y);
//			}
//			//X
//			for(var col :int = 0; col <= walkCols; col++)
//			{
//				var dy :Number = col * walkH;
//				var p3 :Point = new Point(0,dy);
//				p3 = Display45Util.trans90To45(p3);
//				 
//				var p4 :Point = new Point(mapW,dy);
//				p4 = Display45Util.trans90To45(p4);
//				g.moveTo(p3.x,p3.y);
//				g.lineTo(p4.x,p4.y);
//			}
			
//			//菱形文本
//			for(var txtRow :int = 0; txtRow < walkRows; txtRow++)
//			{
//				for(var txtCol :int = 0; txtCol < walkCols; txtCol++)
//				{
//					var tx :Number = txtCol * walkW;
//					var ty :Number = txtRow * walkH;
//					var p :Point = Display45Util.trans90To45(new Point(tx,ty));
//					var txt :TextField = new TextField();
//					txt.selectable = txt.border = false;
//					txt.defaultTextFormat = new TextFormat(null,15,0,null,null,null,null,null,"center");
//					txt.text = txtRow + "," + txtCol;
//					txt.width = 50;
//					txt.height = 30;
//					txt.x = p.x - 25;
//					txt.y = p.y;
//					addChild(txt);
//				}
//			}
			
		}
	}
}